package com.group3.utils;

import com.group3.domain.PunchAttendance;
import com.group3.domain.PunchRecord;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 * @ClassName AttendanceJudge
 * @Author WenhaoChen
 * @Date 2021/6/21 13:24
 **/
public class AttendanceJudge {
    private Iterator<PunchRecord> iterator;

    public AttendanceJudge(){

    }
    public AttendanceJudge(List<PunchRecord> punchRecords){
        iterator = punchRecords.listIterator();
    }

    public PunchAttendance punchJudge(
            PunchAttendance punchAttendance){//使用迭代器方法，每次找到匹配的打卡记录就删除该打卡记录
        PunchAttendance attendance = new PunchAttendance();
        int start = 0;//签到状态，0没有在上课前十分钟内签到，1在上课前十分钟内签到
        int end = 0;//签退状态，0没有在下课后十分钟内签退，1在下课后十分钟内签退

        while (iterator.hasNext()){
            PunchRecord punchRecord = iterator.next();//获取单个打卡记录

            if (start == 1 && end == 1){//已找到在规定时间内的打卡记录
                break;
            }
            if (punchAttendance.getStudentId().equals(punchRecord.getStudentId())
                    && punchAttendance.getEquipment().equals(punchRecord.getEquipment())
                    && punchAttendance.getDate().equals(punchRecord.getPunchDate())){
                Date startTimeLimit = new Date(punchAttendance.getStartTime().getTime()-10*60*1000);
                Date endTimeLimit = new Date(punchAttendance.getEndTime().getTime()+10*60*1000);

                if (startTimeLimit.before(punchRecord.getPunchTime()) //在规定时间内签到
                        && punchAttendance.getStartTime().after(punchRecord.getPunchTime())){
                    start = 1;
                    attendance.setRealStartTime(punchRecord.getPunchTime());//设置为实际签到时间
                    iterator.remove();
                    continue;
                }
                if (endTimeLimit.after(punchRecord.getPunchTime()) //在规定时间内签退
                        && punchAttendance.getEndTime().before(punchRecord.getPunchTime())){
                    end = 1;
                    attendance.setRealEndTime(punchRecord.getPunchTime());//设置为实际签退时间
                    iterator.remove();
                    continue;
                }
            }
        }

        if (start == 1 && end == 1){
            attendance.setAttendanceType(1);//正常签到签退
        }else if (start == 0 && end == 1){
            attendance.setAttendanceType(2);//迟到
        }else if (start == 1 && end == 0){
            attendance.setAttendanceType(3);//早退
        }else {
            attendance.setAttendanceType(5);//旷课
        }
        return attendance;
    }
//
//    public PunchAttendance punchJudge(//使用for循环list的方法，for循环效率优于迭代器
//            PunchAttendance punchAttendance, List<PunchRecord> punchRecords){
//        PunchAttendance attendance = new PunchAttendance();
//        int start = 0;//签到状态，0没有在上课前十分钟内签到，1在上课前十分钟内签到
//        int end = 0;//签退状态，0没有在下课后十分钟内签退，1在下课后十分钟内签退
//
//        for (PunchRecord punchRecord : punchRecords){
//            if (start == 1 && end == 1){//已找到在规定时间内的打卡记录
//                break;
//            }
//            if (punchAttendance.getStudentId().equals(punchRecord.getStudentId())
//                    && punchAttendance.getEquipment().equals(punchRecord.getEquipment())
//                    && punchAttendance.getDate().equals(punchRecord.getPunchDate())){
//                Date startTimeLimit = new Date(punchAttendance.getStartTime().getTime()-10*60*1000);
//                Date endTimeLimit = new Date(punchAttendance.getEndTime().getTime()+10*60*1000);
//
//                if (startTimeLimit.before(punchRecord.getPunchTime()) //在规定时间内签到
//                        && punchAttendance.getStartTime().after(punchRecord.getPunchTime())){
//                    start = 1;
//                    attendance.setRealStartTime(punchRecord.getPunchTime());//设置为实际签到时间
//                    continue;
//                }
//                if (endTimeLimit.after(punchRecord.getPunchTime()) //在规定时间内签退
//                        && punchAttendance.getEndTime().before(punchRecord.getPunchTime())){
//                    end = 1;
//                    attendance.setRealEndTime(punchRecord.getPunchTime());//设置为实际签退时间
//                    continue;
//                }
//            }
//        }
//
//        if (start == 1 && end == 1){
//            attendance.setAttendanceType(1);//正常签到签退
//        }else if (start == 0 && end == 1){
//            attendance.setAttendanceType(2);//迟到
//        }else if (start == 1 && end == 0){
//            attendance.setAttendanceType(3);//早退
//        }else {
//            attendance.setAttendanceType(5);//旷课
//        }
//        return attendance;
//    }
}
